package edu.ntu.cloudroid.xacml.builder.policy;

import java.net.URI;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TimeZone;

import com.sun.xacml.Obligation;
import com.sun.xacml.UnknownIdentifierException;
import com.sun.xacml.attr.BooleanAttribute;
import com.sun.xacml.attr.IntegerAttribute;
import com.sun.xacml.attr.StringAttribute;
import com.sun.xacml.attr.TimeAttribute;
import com.sun.xacml.cond.Apply;
import com.sun.xacml.cond.EqualFunction;
import com.sun.xacml.cond.Evaluatable;
import com.sun.xacml.cond.Function;
import com.sun.xacml.cond.FunctionFactory;
import com.sun.xacml.cond.FunctionTypeException;
import com.sun.xacml.cond.LogicalFunction;
import com.sun.xacml.ctx.Attribute;
import com.sun.xacml.ctx.Result;

import edu.ntu.cloudroid.model.IDs;
import edu.ntu.cloudroid.model.policy.AbstractPrivacyPreference;
import edu.ntu.cloudroid.model.policy.SimpleFiltering;
import edu.ntu.cloudroid.model.policy.SimpleGranularity;
import edu.ntu.cloudroid.model.policy.SimpleSlidingWindow;
import edu.ntu.cloudroid.model.policy.SimpleStats;
import edu.ntu.cloudroid.model.profile.ProfileData;
import edu.ntu.cloudroid.xacml.XACMLStrings;

/**
 * Extending the AbstractPolicyBuilder to define the condition rules used for
 * generating the policy
 * 
 */
public class SimpleGranularityBuilder extends AbstractPolicyBuilder {
	
	SimpleGranularity ss;
	

	@Override
	protected Set<Obligation> createObligations() throws Exception{
		//if filtering based on time constraint, skipped
		ss = (SimpleGranularity)this.preference;
		
		List<Attribute> atts = new ArrayList<Attribute>();
		atts.add(new Attribute(new URI(XACMLStrings.ATTR_Filtering_ColumnIdx), null,
				null, new IntegerAttribute(ss.getDataColumnIndex())));
		
		atts.add(new Attribute(new URI(XACMLStrings.ATTR_Granularity), null,
				null, new IntegerAttribute(ss.getGranualityLevel())));
						
		atts.add(new Attribute(new URI(XACMLStrings.ATTR_GetLatest), null,
				null, BooleanAttribute.getInstance(ss.isLatestDataOnly())));
		
		Obligation filteringObligation = new Obligation(new URI(
				IDs.ObligationID.GranularityObligation.toString()),
				Result.DECISION_PERMIT, atts);
		Set<Obligation> obs = new HashSet<Obligation>();
		obs.add(filteringObligation);
		return obs;
	}
}
